import 'package:inmotion_pro/pages/models/vehicle_error_model.dart';
import 'package:inmotion_pro/service/realtime_data_server/vehicle_realtime_data/vehicle_data_base.dart';
import 'package:safemap/safemap.dart';

// 实时错误
class VehicleRealtimeError extends VehicleDataBase {
  // 警告错误
  List<VehicleErrorModel> vehicleErrors = [];

  VehicleRealtimeError.fromMap(Map<String, dynamic>? data)
      : super.fromMap(data) {
    SafeMap safeMap = SafeMap(data);
    if (safeMap['i_phase_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "前轮相位传感器异常",
      ));
    }
    if (safeMap['i_bus_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "总电流传感器异常",
      ));
    }
    if (safeMap['motor_hall1'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "电机霍尔1异常",
      ));
    }
    if (safeMap['motor_hall2'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "电机霍尔2异常",
      ));
    }
    if (safeMap['battery_error'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "电池异常",
      ));
    }
    if (safeMap['imu_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "姿态传感器异常",
      ));
    }
    if (safeMap['mc_comm1'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "驱动器通讯异常",
      ));
    }
    if (safeMap['mc_comm2'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "驱动器通讯异常",
      ));
    }
    if (safeMap['ble_comm1'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "蓝牙板通讯异常",
      ));
    }
    if (safeMap['ble_comm2'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "蓝牙板通讯异常",
      ));
    }
    if (safeMap['battery_comm_lost'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "电池-驱动板通讯异常",
      ));
    }
    if (safeMap['battery_cross_comm_lost'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "电池间通讯异常",
      ));
    }
    if (safeMap['mos_temp_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "MOS 温控异常",
      ));
    }
    if (safeMap['motor_temp_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "电机温控异常",
      ));
    }
    if (safeMap['board_temp_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "主板高温区温控异常",
      ));
    }
    if (safeMap['fan'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "散热风扇异常",
      ));
    }
    if (safeMap['rtc'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "RTC模块异常",
      ));
    }
    if (safeMap['ext_rom'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "外部存储器异常",
      ));
    }
    if (safeMap['v_bus_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "总电压传感器异常",
      ));
    }
    if (safeMap['v_battery_sensor'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "电池电压传感器异常",
      ));
    }
    if (safeMap['cannot_power_off'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "无法正常断电",
      ));
    }
    if (safeMap['cannot_charge'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "无法充电",
      ));
    }
    if (safeMap['tbox_comm'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.error,
        message: "云盒通信异常",
      ));
    }
    // 处理警告错误
    if (safeMap['under_voltage'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "欠压警告",
      ));
    }
    if (safeMap['over_voltage'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "过压警告",
      ));
    }
    if (safeMap['over_current'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "电流过大警告",
      ));
    } else if (safeMap['over_current'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "低电骑行电流过大警告",
      ));
    } else if (safeMap['over_current'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "低电推行电流过大警告",
      ));
    }
    if (safeMap['low_battery'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "低电等级警告",
      ));
    }
    if (safeMap['mos_over_temp'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "MOS温度过高警告",
      ));
    }
    if (safeMap['motor_over_temp'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "电机温度过高警告",
      ));
    }
    if (safeMap['board_over_temp'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "主板高温区温度过高警告",
      ));
    }
    if (safeMap['cpu_over_temp'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "CPU 温度过高警告",
      ));
    }
    if (safeMap['imu_over_temp'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "IMU 温度过高警告",
      ));
    }
    if (safeMap['over_speed'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "超速警告",
      ));
    }
    if (safeMap['output_saturation'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "矢量电压饱和警告",
      ));
    }
    if (safeMap['motor_spin'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "空转警告",
      ));
    }
    if (safeMap['motor_stall'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "电机堵转警告",
      ));
    }
    if (safeMap['posture_error'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.warning,
        message: "骑行时姿态异常",
      ));
    }
    if (safeMap['risk_behaviour'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "危险骑行动作警告",
      ));
    }
    if (safeMap['motor_no_load'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "电机空载保护警告",
      ));
    }
    if (safeMap['no_self_test'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "必要的自检项未进行或未通过警告",
      ));
    }
    if (safeMap['power_key_long_press'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "电源按键被长时间按下警告",
      ));
    }
    if (safeMap['some_batteries_not_work'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "有部分电池未工作警告",
      ));
    }
    if (safeMap['battery_cali_error'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "电池电压校准异常警告",
      ));
    }
    if (safeMap['battery_warning'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "电池报警",
      ));
    }
    if (safeMap['battery_protect'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "电池保护",
      ));
    }
    if (safeMap['charge_over_current'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "充电过流",
      ));
    }
    if (safeMap['dc_output_over_current'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "直流输出口过流",
      ));
    }
    if (safeMap['sw_combination'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "各组件软件不匹配警告",
      ));
    }
    if (safeMap['force_dfu'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "强制固件升级警告",
      ));
    }
    if (safeMap['device_lock'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "车辆已被锁定警告",
      ));
    }
    if (safeMap['hw_combination'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "各组件硬件不匹配警告",
      ));
    }
    if (safeMap['fan_low_speed'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "风扇转速过低警告",
      ));
    }
    if (safeMap['hw_over_current'].boolean) {
      vehicleErrors.add(VehicleErrorModel(
        type: VehicleErrorType.protected,
        message: "硬件过流保护",
      ));
    }
  }
}

/*
  uint16_t i_phase_sensor          : 1; //0 相电流传感器正常   1 相电流传感器故障
    uint16_t i_bus_sensor            : 1; //0 总电流传感器正常   1 总电流传感器故障
    uint16_t motor_hall1             : 1; //0 电机霍尔1正常      1 电机霍尔1故障
    uint16_t motor_hall2             : 1; //0 电机霍尔2正常      1 电机霍尔2故障
    uint16_t battery_error           : 1; //0 电池正常           1 电池故障（电池内部报错）
    uint16_t imu_sensor              : 1; //0 姿态传感器正常     1 姿态传感器故障
    uint16_t mc_comm1                : 1; //0 驱动器通讯正常     1 驱动器通讯故障（人机板无法收到驱动板数据）
    uint16_t mc_comm2                : 1; //0 驱动器通讯正常     1 驱动器通讯故障（驱动板无法收到人机板数据）
    uint16_t ble_comm1               : 1; //0 蓝牙板通讯正常     1 蓝牙板通讯故障（人机板无法收到蓝牙板数据）
    uint16_t ble_comm2               : 1; //0 蓝牙板通讯正常     1 蓝牙板通讯故障（蓝牙板无法收到人机板数据）
    uint16_t battery_comm_lost       : 1; //0 电池-驱动板通讯正常1 电池-驱动板通讯故障
    uint16_t battery_cross_comm_lost : 1; //0 电池间通讯正常     1 电池间通讯故障
    uint16_t mos_temp_sensor         : 1; //0 MOS 温控正常       1 MOS 温控故障
    uint16_t motor_temp_sensor       : 1; //0 电机温控正常       1 电机温控故障
    uint16_t board_temp_sensor       : 1; //0 主板高温区温控正常 1 主板高温区温控故障
    uint16_t fan                     : 1; //0 散热风扇正常       1 散热风扇故障
    uint16_t rtc                     : 1; //0 RTC模块正常        1 RTC 模块故障
    uint16_t ext_rom                 : 1; //0 外部存储器正常     1 外部存储器故障
    uint16_t v_bus_sensor            : 1; //0 总电压传感器正常   1 总电压传感器故障
    uint16_t v_battery_sensor        : 1; //0 电池电压传感器正常 1 电池电压传感器故障
    uint16_t cannot_power_off        : 1; //0 未发现无法断电异常 1 无法正常断电故障
    uint16_t cannot_charge           : 1; //0 未发现无法充电故障 1 无法充电故障
    uint16_t tbox_comm               : 1; //0 云盒通信正常       1 云盒通信故障
    uint16_t error_reserve           : 9; //0 保留

    //警告 32 位
    uint16_t under_voltage           : 1; //0 电压（母线）正常   1 欠压警告
    uint16_t over_voltage            : 1; //0 电压（母线）正常   1 过压警告
    uint16_t over_current            : 2; //0 电流（母线）正常   1 电流过大警告   2 低电骑行电流过大警告  3 低电推行电流过大警告
    uint16_t low_battery             : 2; //0 电量正常           1~3 低电等级1~3
    uint16_t mos_over_temp           : 1; //0 MOS温度正常        1 MOS温度过高警告
    uint16_t motor_over_temp         : 1; //0 电机温度正常       1 电机温度过高警告
    uint16_t board_over_temp         : 1; //0 主板高温区温度正常 1 主板高温区温度过高警告
    uint16_t cpu_over_temp           : 1; //0 CPU 温度正常       1 CPU 温度过高警告
    uint16_t imu_over_temp           : 1; //0 IMU 温度正常       1 IMU 温度过高警告
    uint16_t over_speed              : 1; //0 未超速             1 超速警告
    uint16_t output_saturation       : 1; //0 矢量电压未饱和     1 矢量电压饱和警告
    uint16_t motor_spin              : 1; //0 未空转             1 空转警告
    uint16_t motor_stall             : 1; //0 电机未堵转         1 电机堵转警告
    uint16_t posture_error           : 1; //0 骑行时姿态正常     1 倾角过大警告

    uint16_t risk_behaviour          : 1; //0 骑行状态正常       1 危险骑行动作警告
    uint16_t motor_no_load           : 1; //0 未检测到空载       1 电机空载保护警告
    uint16_t no_self_test            : 1; //0 已通过必要的自检   1 存在必要的自检项未进行或未通过警告
    uint16_t power_key_long_press    : 1; //0 电源按键未发现异常 1 电源按键被长时间按下警告
    uint16_t some_batteries_not_work : 1; //0 未发现有电池不工作 1 有部分电池未工作警告
    uint16_t battery_cali_error      : 1; //0 未发现电池电压校准异常 1 电池电压校准异常警告
    uint16_t battery_warning         : 1; //0 电池无报警         1 电池报警
    uint16_t battery_protect         : 1; //0 电池未保护         1 电池保护
    uint16_t charge_over_current     : 1; //0 充电未过流         1 充电过流（由驱动板检测的过流）
    uint16_t dc_output_over_current  : 1; //0 直流输出口未过流   1 直流输出口过流
    uint16_t sw_combination          : 1; //0 未发现软件不匹配   1 各组件软件不匹配警告
    uint16_t force_dfu               : 1; //0 无强制固件升级     1 强制固件升级警告
    uint16_t device_lock             : 1; //0 未锁定             1 车辆已被锁定警告
    uint16_t hw_combination          : 1; //0 未发现硬件不匹配   1 各组件硬件不匹配警告
    uint16_t fan_low_speed           : 1; //0 未发现风扇转速过低 1 风扇转速过低警告（会使前灯最大亮度被迫降低）
    uint16_t hw_over_current         : 1; //0 未出现硬件过流     1 硬件过流保护

*/
